package com.moleq.dialog;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.moleq.adapter.SelectionAdapter;
import com.moleq.base.MoleQBaseDialog;
import com.moleq.base.OnResultListener;
import com.moleq.logic.MoleQConstants;
import com.moleq.mgpos.activities.R;
import com.moleq.models.AdapterDataSourceBuilder;
import com.moleq.models.Departmant;
import com.moleq.service.ServiceManager;

public class DeptSelectionDialog extends MoleQBaseDialog {

	private OnResultListener mOnResultListener;
	private Button mButtonBack, mButtonOk;
	private Context mContext;
	private ListView mListView;
	private List<Departmant> mDataSource = new ArrayList<Departmant>();
	private SelectionAdapter mAdapter;
	private int selectedRow = 0;

	private Button.OnClickListener mOnClickListener = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_dialog_cancel:
				dismiss();
				break;
			case R.id.btn_dialog_ok:
				if (mDataSource.size() <= 0) {
					return;
				}
				onCloseWithReturn(mDataSource.get(selectedRow));
				dismiss();
				break;
			}
		}
	};

	public ListView.OnItemClickListener onItemClickListener = new ListView.OnItemClickListener() {

		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

			switch (parent.getId()) {
			case R.id.lv_dialog_dept_list:
				selectedRow = position;
				mAdapter.ScrollToRow(position);
				break;
			}
		}
	};

	public void onCloseWithReturn(Object obj) {
		mOnResultListener.onReturnValue(MoleQConstants.DIALOG_PRICE_DEPT_INFO, obj);
		dismiss();
	}

	public DeptSelectionDialog(Context context) {
		super(context, context);
		constructor(context);

	}

	public DeptSelectionDialog(Context context, Object base) {
		super(context, base);
		constructor(context, base);
	}

	private void constructor(Context context, Object base) {
		mContext = context;
		mOnResultListener = (OnResultListener) base;
	}

	public DeptSelectionDialog(Context context, int theme) {
		super(context, theme);
		constructor(context);
	}

	private void constructor(Context context) {
		// TODO Auto-generated method stub
		mContext = context;
	}

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// set UI layout
		this.setContentView(R.layout.price_dept_dialog_perview_models);
		this.setCancelable(false);

		mButtonBack = (Button) findViewById(R.id.btn_dialog_cancel);
		mButtonBack.setOnClickListener(mOnClickListener);

		mButtonOk = (Button) findViewById(R.id.btn_dialog_ok);
		mButtonOk.setOnClickListener(mOnClickListener);

		mListView = (ListView) findViewById(R.id.lv_dialog_dept_list);
		mListView.setOnItemClickListener(onItemClickListener);

	}

	public void show(String dep_name) {

		super.show();

		mDataSource = ServiceManager.getInstance().mDepartmantService.FindAll();
		if (mDataSource.size() <= 0) {
			Departmant dep = new Departmant();
			dep.setDep_name("Department1");
			dep.setDep_no(1);
			mDataSource.add(dep);
		}

		mAdapter = new SelectionAdapter(mContext, AdapterDataSourceBuilder.readValueFromDep(mDataSource));
		mListView.setAdapter(mAdapter);
		int position = GetDeptPosition(dep_name);
		if(mDataSource.size() > 0 && position == -1)position = 0;
		mAdapter.ScrollToRow(position);
//		if(mDataSource.size()>0)mAdapter.ScrollToRow(0);
		// mListView.setDivider(null);

		
	}
	
	public int GetDeptPosition(String DeptName) {
		for (int i = 0; i < mDataSource.size(); i++) {
			Departmant dept = mDataSource.get(i);
			if (dept.getDep_name().equals(DeptName)) {
				return i;
			}
		}
		return -1;
	}

}
